#include <tinyhal.h>
#include "..\..\..\DeviceCode\Targets\Native\FM4\DeviceCode\FM4.h"

#ifdef __GNUC__
void __attribute__((section("SectionForBootstrapOperations"))) BootstrapCode_GPIO()
#else
void __section(SectionForBootstrapOperations) BootstrapCode_GPIO()
#endif
{
#if defined(MB9BF568R)
    volatile uint32_t t;
    FM4_GPIO->ADE       = 0x00000000;

    // CS8 - SDRAM 32MB
    // Micron MT48LC16M16A2 Column 512[8:0] x Row 8K[12:0] x Bank 4[1:0]
    // BASEL: 100 [23:24]
    // RASEL: 100 [10:22]
    // CASEL: 0   [01:09]
    // CS8 - SDRAM 16MB
    // Micron MT48LC16M16A2 Column 512[8:0] x Row 8K[12:0] x Bank 4[1:0]
    // BASEL: 011 [22:23]
    // RASEL: 100 [10:21]
    // CASEL: 0   [01:09]
    FM4_EXBUS->SDMODE = 0x00010000;     //MSDCLKOFF=1, SDON=0(OFF)
    FM4_EXBUS->DCLKR    = (0 << 4)      //MCLKON
                        | (4 << 0);     //MDIV 1/5 160MHz/5 = 32MHz
#if SRAM1_MEMORY_Size == 0x02000000
    FM4_EXBUS->SDMODE |= 0x00004400;    // 32MB
#elif SRAM1_MEMORY_Size == 0x01000000
    FM4_EXBUS->SDMODE |= 0x00003400;    // 16MB
#else
    FM4_EXBUS->SDMODE |= 0x00003400;    // 16MB
#endif
    FM4_EXBUS->REFTIM = 0x00020033;     //NREF=3,REFC=33
    //FM4_EXBUS->SDTIM = 0x00421151;
    FM4_EXBUS->SDTIM = 0x00421141;
    FM4_EXBUS->PWRDWN = 0xffff;
    FM4_EXBUS->AMODE = 0x0000;
    // GPIO
#if defined(ENABLE_NAND)
    FM4_GPIO->PFR0      |= 0x6C00;      //P0D:MDQM0,P0E:MDQM1,P0A:CS1, P0B:CS0
#else
    FM4_GPIO->PFR0      |= 0x6400;      //P0D:MDQM0,P0E:MDQM1,P0A:CS1
#endif
    //FM4_GPIO->PFR1      = 0x05ff;       //P10...P18/A7...A15,A17
    FM4_GPIO->PFR1      = 0x01ff;       //P10...P18/A7...A15
#if defined(ENABLE_NAND)
    FM4_GPIO->PFR3      = 0xfeff;
#else
    FM4_GPIO->PFR3      = 0xfeff;
#endif
    FM4_GPIO->PFR4      = 0x780c;
    FM4_GPIO->PFR5      = 0x0fff;
    FM4_GPIO->PFR6      = 0x000c;
#if defined(ENABLE_NAND)
    FM4_GPIO->EPFR10    = 0x0fffc0fb;   //A17,CS1,OE,WE, UEFLSE
    FM4_GPIO->EPFR11    = 0x01fffffe;   //CS0
#else
    FM4_GPIO->EPFR10    = 0x0fffc0bb;   //A17,CS1,OE,WE
    FM4_GPIO->EPFR11    = 0x01fffffc;
#endif
    FM4_GPIO->EPFR20    = 0x0000003f;

    FM4_EXBUS->SDMODE &= 0xfffeffff;
    t = 1000;
    while (t-- >0);                     //wait for clk stability
    FM4_EXBUS->SDMODE |= 0x00000001;

#if defined(ENABLE_NAND)
    // CS0 - NAND
    FM4_EXBUS->MODE0    = (0 << 0)      //WDTH 8bit
                        | (1 << 4);     //NAND on
    FM4_EXBUS->TIM0     = (0 << 28)     //WIDLC
                        | (0 << 24)     //WWEC
                        | (0 << 20)     //WADC
                        | (2 << 16)     //WACC
                        | (0 << 12)     //RIDLC
                        | (0 << 8)      //FRADC
                        | (1 << 4)      //RADC
                        | (2 << 0);     //RACC
    FM4_EXBUS->AREA0    = (0x1f << 16)  //MASK
                        | (0x00 << 0);  //ADDR 0x60000000
#endif
    // CS1 - LCD
    FM4_EXBUS->MODE1    = (0 << 0)      //WDTH 8bit
                        | (1 << 2);     //RBMON
    FM4_EXBUS->TIM1     = (0 << 28)     //WIDLC
                        | (2 << 24)     //WWEC
                        | (0 << 20)     //WADC
                        | (2 << 16)     //WACC
                        | (1 << 12)     //RIDLC
                        | (0 << 8)      //FRADC
                        | (0 << 4)      //RADC
                        | (6 << 0);     //RACC
    FM4_EXBUS->AREA1    = (0x0f << 16)  //MASK 2MB
                        | (0x10 << 0);  //ADDR 0x61000000

#endif
}
